<?php

namespace app\common\model\sign;

use app\common\model\BaseModel;

class Sign extends BaseModel
{

    // 定义表名
    protected $name = 'sign';

    protected $createTime = false;
    protected $updateTime = false;

    // 定义主键
    protected $pk = 's_id';

    /**
     * 关联会员记录表
     * @return \think\model\relation\BelongsTo
     */
    public function user()
    {
        return $this->belongsTo("app\\common\\model\\User", 'user_id', 'user_id');
    }


    public function getList($param = [], $with = []): array
    {
        $where = [];
        if (!empty($param['user_id']) && $param['user_id'] > 0) {
//            $this->where('user_id', '=', (int)$param['user_id']);
            $where[] = ['user_id', '=', (int)$param['user_id']];
        }

        if (isset($param['start_time']) && !empty($param['start_time'])) {
//            $this->where('last_sign_time', '>=', strtotime($param['start_time']));
            $where[] = ['last_sign_time', '>=', strtotime($param['start_time'])];
        }
        if (isset($param['end_time']) && !empty($param['end_time'])) {
//            $this->where('last_sign_time', '<', strtotime($param['end_time']) + 86400);
            $where[] = ['last_sign_time', '<', strtotime($param['end_time']) + 86400];
        }

        return $this->when((count($where) > 0), function ($query) use ($where) {
            $query->where($where);
        })->when((count($with) > 0), function ($query) use ($with) {
            $query->with($with);
        })->order(['s_id' => 'desc'])->paginate()->toArray() ?? [];
    }
}
